package ControlLayer;
import ModelLayer.*;

/**
 * Write a description of class BorrowCtr here.
 * 
 * @author Rasmus Andersen Borup 
 * @version 
 */
public class BorrowCtr
{
    // instance variables
    private BorrowContainer borrowContainer;

    /**
     * Constructor for objects of class BorrowCtr
     */
    public BorrowCtr()
    {
        // initialising instance variables
        borrowContainer = BorrowContainer.getInstance();
    }

    private Borrow findBorrow(int dvdId)
    {
        return borrowContainer.getBorrow(dvdId);
    }
    
    private Borrow createBorrow(long id, int dvdId, String startDate, String endDate, String status)
    {
                //creates a Person object and stores it in the container
        Borrow borrow = null;
        boolean found = false;
        int i = 0;
        if(borrowContainer.getBorrows().size() == 0)
        {
            borrow = new Borrow(id, dvdId, startDate, endDate, status);
        }
        else
        {
            while(!found && i < borrowContainer.getBorrows().size())
            {
                if(borrowContainer.getBorrows().get(i).getDvdId() == dvdId)
                {
                    found = true;
                }
                else
                {
                    i++;
                }
            }
            if(!found)
            {
                borrow = new Borrow(id, dvdId, startDate, endDate, status);
            }
        }
        return borrow;
    }
    
    public void addBorrow(long id, int dvdId, String date, String period, String status)
    {
        borrowContainer.addBorrow(createBorrow(id, dvdId, date, period, status));
    }
    
    public void deleteBorrow(int dvdId)
    {
        borrowContainer.removeBorrow(dvdId);
    }
}
